﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Globalization;
using System.IO;
using ReToolNaming.Classes;

namespace ReToolNaming
{
    public partial class Form1 : Form
    {
        private RenameUtils renameUtils;
        
        public Form1()
        {
            InitializeComponent();
            renameUtils = new RenameUtils();
            renameUtils.InitializeUtils() ;
            tbSearchPatternLeft.Text = "*.avi";
            tbSearchPatternRight.Text = "*.srt";
            lvFilesLeft.AllowDrop = true;
            lvFilesRight.AllowDrop = true;
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            log.addLogTextLine("Application opened.");
            RenameUtils.PopulateFiles(lvFilesLeft, tbSearchPatternLeft.Text, folderBrowserDialog1);
            RenameUtils.PopulateFiles(lvFilesRight, tbSearchPatternRight.Text, folderBrowserDialog1);
            log.addLogTextLine("Populating form with content from path: " + RenameUtils.getFullPath(folderBrowserDialog1).ToString());
        }

        private void bFilterLeft_Click(object sender, EventArgs e)
        {
            RenameUtils.PopulateFiles(lvFilesLeft, tbSearchPatternLeft.Text, folderBrowserDialog1);
        }

        private void bFilterRight_Click(object sender, EventArgs e)
        {
            RenameUtils.PopulateFiles(lvFilesRight, tbSearchPatternRight.Text, folderBrowserDialog1);
        }

        private void bMatch_Click(object sender, EventArgs e)
        {
            renameUtils.MatchLeftRight(lvFilesLeft, lvFilesRight);
            log.addLogTextLine("Manually matched files");
        }

        private void bRenameFromLeft_Click(object sender, EventArgs e)
        {
            renameUtils.Rename(RenameUtils.DIRECTION_LEFT, lvFilesLeft, lvFilesRight, folderBrowserDialog1, tbSearchPatternLeft.Text, tbSearchPatternRight.Text);
        }

        private void bRenameFromRight_Click(object sender, EventArgs e)
        {
            renameUtils.Rename(RenameUtils.DIRECTION_RIGHT, lvFilesLeft, lvFilesRight, folderBrowserDialog1, tbSearchPatternLeft.Text, tbSearchPatternRight.Text);
        }

        private void lvFilesLeft_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                bMatch.PerformClick();

        }

        private void bAutoDetect_Click(object sender, EventArgs e)
        {
            renameUtils.AutoMatchListViews(lvFilesLeft, lvFilesRight, pbProgres, cbUseComplexRegex.Checked, tbRegex.Text.ToString(), cbSpeed.Checked);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            RenameUtils.PopulateFiles(lvFilesLeft, tbSearchPatternLeft.Text, folderBrowserDialog1);
            RenameUtils.PopulateFiles(lvFilesRight, tbSearchPatternRight.Text, folderBrowserDialog1);
            log.addLogTextLine("Populating form with content from path: " + RenameUtils.getFullPath(folderBrowserDialog1).ToString());

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 aboutBox = new AboutBox1();
            aboutBox.ShowDialog();
            aboutBox.Dispose();
        }

        private void cbUseComplexRegex_CheckedChanged(object sender, EventArgs e)
        {
            tbRegex.Enabled = cbUseComplexRegex.Checked;
        }

        private void button1_Click(object sender, EventArgs e)//Clear matches.
        {
            RenameUtils.ClearMathingLeftRight(lvFilesLeft, lvFilesRight);
        }

        private void lvFilesLeft_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            lvFilesLeft.ItemChecked -= lvFilesLeft_ItemChecked;
            RenameUtils.BehaveLikeRadioCheckBox(lvFilesLeft, e.Item);
            lvFilesLeft.ItemChecked += lvFilesLeft_ItemChecked;
        }

        private void lvFilesRight_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            lvFilesRight.ItemChecked -= lvFilesRight_ItemChecked;
            RenameUtils.BehaveLikeRadioCheckBox(lvFilesRight, e.Item);
            lvFilesRight.ItemChecked += lvFilesRight_ItemChecked;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            foreach (string file in files)
            {
                bool flagPass = false;
                if (Directory.Exists(file.ToString()))
                {
                    folderBrowserDialog1.SelectedPath = file.ToString();
                    flagPass = true;
                }
                if (File.Exists(file.ToString()))
                {
                    FileInfo ddFile = new FileInfo(file.ToString());
                    folderBrowserDialog1.SelectedPath = ddFile.Directory.FullName.ToString();
                    flagPass = true;
                }
                if (flagPass)
                {
                    RenameUtils.PopulateFiles(lvFilesLeft, tbSearchPatternLeft.Text, folderBrowserDialog1);
                    RenameUtils.PopulateFiles(lvFilesRight, tbSearchPatternRight.Text, folderBrowserDialog1);
                    log.addLogTextLine("Populating form with content from path: " + RenameUtils.getFullPath(folderBrowserDialog1).ToString());
                    break;
                }
            }
            
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                e.Effect = DragDropEffects.All;
            }  
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            log.addLogTextLine("Application closed.");
        }

    }
}
